<?php
require("../e/class/connect.php");
require("../e/class/db_sql.php");
require("../e/data/dbcache/class.php"); //引入栏目缓存文件
require("../e/extend/wechat/wechat.class.php");
require("../e/extend/wechat/wechat.config.php");
require("../e/extend/wechat/wechat.fun.php");

//file_put_contents('wxSend_date.txt',date("Y-m-d H:i:s").PHP_EOL,FILE_APPEND);

$send=(int)$_GET['send'];
$wxSendSwitchFile='../e/extend/wechat/tokenCache/wxSend_'.$send.'_'.date("Y-m-d").'.txt';
//获取任务状态
$switch=file_get_contents($wxSendSwitchFile);
//任务进行中，退出
//if($switch==1) exit;
//标注任务开始
//file_put_contents($wxSendSwitchFile,1);

$link=db_connect();
$empire=new mysqlquery();
if($send==1||$send==2)
{
	addSendShangKe($send);
}
else if($_GET['send']==3)
{
	wxSend();
}

//$WeiXin = new Wechat($options);
//加入通知列队
//标注任务结束
//file_put_contents($wxSendSwitchFile,0);
//导入模板
db_close();
$empire=null;
//提醒通知进入列队
function addSendShangKe($send=1){
	global $options,$empire;

	$bqno    = 0;
	$date    = date("Y-m-d H:i:s");
	$w       = date("w");
	$tempids = $options['tempid'];
	$wehre   = $send==2?"sendTime2":"sendTime1";
	$shangKeField   = $send==2?"shangKeTime2":"shangKeTime1";
	if($_GET['debug'])
	{
		echo "<br>";
		echo whereSQL($options['student'],0,'id,uidName,tidName,tid,openid,openid1,topenid,week1,time1,week2,time2,keMu,sendTime1,sendTime2').$wehre."<'$date' order by id DESC limit 200";
		echo "<br>";
	}

	$sql=$empire->query(whereSQL($options['student'],0,'id,uidName,tidName,tid,openid,openid1,topenid,week1,time1,week2,time2,keMu,sendTime1,sendTime2').$wehre."<'$date' order by id DESC limit 200");
	while($r=$empire->fetch($sql))
	{
		++$bqno;
		$time  = time();
		$url   = "http://".$_SERVER['HTTP_HOST']."/webapp/?ac=jiazhang";
		$array = array();
		$color = array();
		$array['first']    = "通知家长！";
		$color['first']    = "#d9534f";
		$array['keyword1'] = "报备通知";
		$color['keyword1'] = "#000";
		$array['keyword2'] = "等待报备";
		$color['keyword2'] = "#000";
		$array['keyword3'] = date("Y-m-d H:i",$r['newstime'])."\n客户姓名：".$r['title']."\n业务姓名：".$r['yid_name']."\n业务电话：".$r['yid_phone']."\n中介名称：".$r['zid_name']."\n门店名称：".$r['mid_name'];
		$color['keyword3'] = "#000";
		$array['remark']   = "查看详情";
		$color['remark']   = "#5bc0de";
		
		$param = array();
		$param['classid'] = $options['wxsend'];
		$param['tid']     = $r['tid'];
		$param['uid']     = $r['id'];
		$param['tidName'] = $r['tidName'];
		$param['uidName'] = $r['uidName'];
		$param['openid']  = $r['openid'];
		$param['week1']   = $send==2?$r['week2']:$r['week1'];
		$param['time1']   = $send==2?$r['time1']:$r['time1'];
		if($_GET['debug'])
		{
			echo "<pre>";
			print_r($r);
			print_r($param);
			echo "<br>";
		}
		if($param['week1']&&$param['time1'])
		{
			$sendJiaZhang='[未通知家长]';
			//家长
			if($r['openid'])
			{
				$sendJiaZhang='[已通知家长]';
				$sendData = sendTemplateMessage_date($r['openid'],$tempids[0],$url,$array,$color);
				$sendData = json_encode($sendData, JSON_UNESCAPED_UNICODE);
				$param['title']       = '通知家长';
				$param['sendData']    = $sendData;
				$param['isonly']      = 'uid1-'.$r['id'].'-'.$r['tid'].'-'.$send.'-'.date("z");
				$result=ECMS_Infor_Curl($param);
				if($_GET['debug'])
				{
					echo "<pre>";
					print_r($result);
					echo "<br>";
				}
			}
			//家长
			if($r['openid1'])
			{
				$sendJiaZhang='[已通知家长]';
				$sendData = sendTemplateMessage_date($r['openid1'],$tempids[0],$url,$array,$color);
				$sendData = json_encode($sendData, JSON_UNESCAPED_UNICODE);
				$param['title']       = '通知家长';
				$param['sendData']    = $sendData;
				$param['isonly']      = 'uid2-'.$r['id'].'-'.$r['tid'].'-'.$send.'-'.date("z");
				$result=ECMS_Infor_Curl($param);
				if($_GET['debug'])
				{
					echo "<pre>";
					print_r($result);
					echo "<br>";
				}
			}
			//老师
			if($r['topenid'])
			{
				$url   = "http://".$_SERVER['HTTP_HOST']."/webapp/";
				$array['first']    = "通知教师";
				$sendData = sendTemplateMessage_date($r['topenid'],$tempids[0],$url,$array,$color);
				$sendData = json_encode($sendData, JSON_UNESCAPED_UNICODE);
				//写数据
				$param['openid']   = $r['topenid'];
				$param['title']    = '通知教师';
				$param['sendData'] = $sendData;
				$param['isonly']   = 'tid-'.$r['id'].'-'.$r['tid'].'-'.$send.'-'.date("z");
				$result=ECMS_Infor_Curl($param);
				if($_GET['debug'])
				{
					echo "<pre>";
					print_r($result);
					echo "<br>";
				}
			}
			$sendTime = getWeekSend($param['week1'],1).' '.$param['time1'];
			//$shangKeTime = strtotime(date("Y-m-d").' '.$param['time1'].':00')+3600+(86400*($param['week1']-$w));
			//$shangKeTime = date("Y-m-d H:i:s",$shangKeTime);
		}
		else
		{
			$sendTime = "2038-01-19 02:14:08";
		}
		if($_GET['debug'])
		{
			echo "<br>";
			echo whereSQL($options['student'],2).$wehre."='$sendTime' where id='$r[id]'";
			echo "<br>";
		}
		$empire->query(whereSQL($options['student'],2).$wehre."='$sendTime' where id='$r[id]'");
	}
	if(!$bqno)
	{
		if($_GET['debug']) echo '不需要通知';
	}
}
//发送模板消息
function wxSend(){
	global $empire,$options;
	$weObj = new Wechat($options);
	$bqno=0;
	$sql=$empire->query(whereSQL($options['wxsend'],0,'id,sendData')."del=0 order by id DESC limit 200");

	while($r=$empire->fetch($sql))
	{
		++$bqno;
		$sendData = json_decode(stripslashes($r['sendData']),true);
		
		$res = $weObj->sendTemplateMessage($sendData);
		if($res)
		{
			$errCode=0;
			$errMsg=$res['msgid'];
		}
		else
		{
			$errCode = $weObj->errCode;
			$errMsg  = $weObj->errMsg;
		}
		if($_GET['debug'])
		{
			echo whereSQL($options['student'],2)."del=1,errCode='$errCode',errMsg='$errMsg' where id='$r[id]'<br>";
			print_r($sendData);
		}
		
		$empire->query(whereSQL($options['student'],2)."del=1,errCode='$errCode',errMsg='$errMsg' where id='$r[id]'");
	}
	if($_GET['debug']) echo "共计发送:".$bqno."条";
}

/*

http://test.eduhz.com/webapp/wxSend.php?send=1&debug=1
http://test.eduhz.com/webapp/wxSend.php?send=2&debug=1
http://test.eduhz.com/webapp/wxSend.php?send=3&debug=1


MAILTO=""
* * * * * /usr/bin/curl http://dztc.dztcapp.com/webapp/wxSend.php?send=1 >> /dev/null 2>&1
* * * * * /usr/bin/curl http://dztc.dztcapp.com/webapp/wxSend.php?send=2 >> /dev/null 2>&1

*/